package com.pozu.new_retail.controllers;

import com.pozu.new_retail.dao.base.JsonResult;
import com.pozu.new_retail.dao.model.CardTraceItem;
import com.pozu.new_retail.service.CardTracesItemService;
import com.pozu.new_retail.utils.CodeConstants;
import com.pozu.new_retail.utils.CodeProperties;
import com.pozu.new_retail.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * 名片 浏览记录 Coltroller
 *
 * @author shenxueyuan
 * @Date 2019/3/15
 */
@Controller
public class CardTracesController {

    @Autowired
    private CardTracesItemService cardTracesItemService;

    /**
     * 查询操作流水记录
     *
     * @author shenxueyuan
     * @Date 2019/3/20
     */
    @ResponseBody
    @RequestMapping(value = "/api/card/traces/list", method = RequestMethod.GET)
    public Object query(HttpServletRequest request) throws Exception {

        int appId = Integer.parseInt(request.getHeader("appId"));//哪个小程序
        String staffId = request.getParameter("staffId");//工作人员id
        //类型 1:访问名片记录 2:点赞名片记录 3:同意某标签记录名片
        String type = request.getParameter("type");

        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("appId", appId);
        hashMap.put("staffId", staffId);
        hashMap.put("type", type);

        JsonResult jr = new JsonResult();

        jr.respCode = CodeConstants.SUCCESS_CODE;
        jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);

        jr.respData = cardTracesItemService.queryTracesByMap(hashMap);
        return jr;
    }


    /**
     * 添加一条操作流水
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/api/card/traces/add", method = RequestMethod.POST)
    public Object add(HttpServletRequest request) throws Exception {

        int appId = Integer.parseInt(request.getHeader("appId"));// 小程序id
        long userId = Long.parseLong(request.getHeader("userId"));// 用户id

        JsonResult jr = new JsonResult();

        CardTraceItem traceItem = Tools.getRequestObject(request, CardTraceItem.class);
        traceItem.setUserId(userId);
        traceItem.setAppId(appId);
        traceItem.setCreateTime(System.currentTimeMillis());

        jr.respCode = CodeConstants.SUCCESS_CODE;
        jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);

        cardTracesItemService.insertCardTraces(traceItem);

        return jr;
    }

    /**
     * 删除操作记录
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/api/card/tag/del", method = RequestMethod.PUT)
    public Object put(HttpServletRequest request) throws Exception {

        long userId = Long.parseLong(request.getHeader("userId"));

        CardTraceItem traceItem = Tools.getRequestObject(request, CardTraceItem.class);

        // 更新state=1
        CardTraceItem tmpTrace = new CardTraceItem();
        tmpTrace.setId(traceItem.getId());
        tmpTrace.setState(1);
        JsonResult jr = new JsonResult();
        cardTracesItemService.updateTraceItem(tmpTrace);

        jr.respCode = CodeConstants.SUCCESS_CODE;
        jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);

        return jr;
    }


}
